<template>
  <NuxtLayout name="admin" menuActive="/admin/role">
    <div class="card">
      <el-page-header @back="useRouter().go(-1)">
        <template #extra>
          <el-button @click="addFn">添加角色</el-button>
        </template>
      </el-page-header>
      <div style="height: 18px;"></div>
      <el-table :data="tableData" border show-overflow-tooltip>
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="authList" label="菜单栏目">
          <template #default="scope">
            {{ (scope.row.authList && scope.row.authList.length > 0) ? scope.row.authList.map(x => x.name).join('，') : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="note" label="备注" />
        <el-table-column label="是否启用">
          <template #default="scope">
            <el-tag type="success" v-show="scope.row.enabled == 1">启用</el-tag>
            <el-tag type="info" v-show="scope.row.enabled == 0">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="140">
          <template #default="scope">
            <el-button type="success" size="small" @click="editFn(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="card_pagin">
        <el-pagination background layout="prev, pager, next, total" :total="total" :page-size="listForm.pageSize"
          @change="pageChangeFn" />
      </div>
    </div>
    <MyForm :visible="formVisible" :menuData="menuData" :forms="forms" :title="formTitle" @pick="formPickFn" />
  </NuxtLayout>
</template>

<script setup>
import { institutionRolePageList, institutionAuthList, addRole, updateRole, deleteRole } from '@/server/api'
import MyForm from './form'
// TDK
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
useSeoMeta({
  title: "技考邦_角色管理",
  keywords: "技考邦,角色管理",
  description: "技考邦，角色管理。",
  ogTitle: "技考邦_角色管理",
  ogDescription: "技考邦，角色管理。",
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
// 列表
const listForm = ref({
  pageNum: 1,
  pageSize: 20
})
const tableData = ref([])
const total = ref(0)
const getList = async () => {
  let { data: listRes } = await useAsyncData('institutionRolePageList', () => useServerRequest(institutionRolePageList, {
    method: 'post',
    body: JSON.stringify(listForm.value)
  }))
  if (listRes.value && listRes.value.errno === 0) {
    tableData.value = listRes.value.result.list;
    total.value = listRes.value.result.total;
  }
}
getList()
// 分页
const pageChangeFn = (val) => {
  listForm.value.pageNum = val;
  getList()
}
// 获取菜单栏目
const menuData = ref([])
const authIds = ref([])
const getMenuData = async () => {
  let { data: menuRes } = await useAsyncData('institutionAuthList', () => useServerRequest(institutionAuthList, {
    method: 'get'
  }))
  if (menuRes.value && menuRes.value.errno === 0) {
    menuRes.value.result.forEach(item => {
      item.label = item.name;
      item.key = item.id;
      // 设置默认导航
      if (item.viewPath == '/admin') {
        item.disabled = true;
        authIds.value = [item.id];
      }
    })
    menuData.value = menuRes.value.result;
  }
}
getMenuData()
// 添加
const forms = ref({})
const formVisible = ref(false)
const formTitle = ref('')
const addFn = () => {
  forms.value.id = null;
  forms.value.name = '';
  forms.value.sort = 0;
  forms.value.authIds = authIds.value;
  forms.value.note = '';
  forms.value.enabled = true;
  formTitle.value = '添加角色';
  formVisible.value = true;
}
// 编辑
const editFn = (val) => {
  forms.value.id = val.id;
  forms.value.name = val.name;
  forms.value.sort = val.sort;
  forms.value.authIds = val.authIds;
  forms.value.note = val.note;
  forms.value.enabled = val.enabled;
  formTitle.value = '编辑角色';
  formVisible.value = true;
}
// 删除
const delFn = (val) => {
  ElMessageBox.confirm('是否删除当前数据', {
    showClose: false,
    closeOnClickModal: false,
    closeOnPressEscape: false
  }).then(async () => {
    let delRes = await useServerRequest(deleteRole, {
      method: 'post',
      body: JSON.stringify({
        id: val.id
      })
    })
    if (delRes.errno === 0) {
      ElMessage({
        message: '删除成功',
        type: 'success'
      })
      getList()
      return false;
    }
  }).catch(() => {})
}
// 取消 确认
const formPickFn = async (e) => {
  if (e.form) {
    const loading = ElLoading.service({ text: '请求中...', target: document.querySelector('.el-dialog') })
    let url = e.form.id ? updateRole : addRole;
    let classRes = await useServerRequest(url, {
      method: 'post',
      body: JSON.stringify(e.form)
    })
    if (classRes.errno === 0) {
      ElMessage({
        message: e.form.id ? '修改成功' : '添加成功',
        type: 'success'
      })
      getList()
      formVisible.value = e.visible;
    }
    loading.close()
    return false;
  }
  formVisible.value = e.visible;
}
</script>

<style lang="less" scoped>
.card {
  background: #FFFFFF;
  border-radius: 10px;
  border: 1px solid #EEEEEE;
  padding: 35px 25px;

  &_pagin {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>